package org.marsdonne.auth;

import org.marsdonne.archives.OrganizationType;
import org.marsdonne.archives.Platform;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class AggregateUserImpl implements AggregateUser {

    @Resource
    private PlatformUserRepos platformUserRepos;

    @Resource
    private UnionUserRepos unionUserRepos;


    @Override
    @Transactional
    public PlatformUser bind(EndPointUser user, Platform platform, String platformId, String phoneNumber, String invoiceTitle) {
        PlatformUser platformUser = null;

        Optional<PlatformUser> optionalPlatformUser = this.platformUserRepos.findByAndPlatformAndPlatformId(platform, platformId);
        if (optionalPlatformUser.isPresent()) {
            platformUser = optionalPlatformUser.get();
            platformUser.setPhoneNumber(phoneNumber);
            platformUser.setInvoiceTitle(invoiceTitle);
        } else {
            platformUser = new PlatformUser(platform, platformId, phoneNumber, invoiceTitle, Arrays.asList(user));
        }

        if (user.getUnionUser() != null)
            platformUser.setUnionUser(user.getUnionUser());

        return this.platformUserRepos.save(platformUser);
    }

    @Override
    @Transactional
    public Optional<UnionUser> bind(String realName, String residentId, OrganizationType organizationType, Long organizationId, String organizationLicenseCode, String organizationName) {
        UnionUser unionUser = this.unionUserRepos.findByResidentId(residentId).orElse(new UnionUser());

        if (realName != null)
            unionUser.setRealName(realName);
        if (residentId != null)
            unionUser.setResidentId(residentId);
        if (organizationType != null)
            unionUser.setOrganizationType(organizationType);
        if (organizationId != null)
            unionUser.setOrganizationId(organizationId);
        if (organizationLicenseCode != null)
            unionUser.setOrganizationLicenseCode(organizationLicenseCode);
        if (organizationName != null)
            unionUser.setOrganizationName(organizationName);

        return Optional.ofNullable(this.unionUserRepos.save(unionUser));
    }

    @Override
    public Optional<UnionUser> unbind(String residentId, String licenseCode) {
        Optional<UnionUser> optionalUnionUser = this.unionUserRepos.findByResidentIdAndOrganizationLicenseCode(residentId, licenseCode);

        if (optionalUnionUser.isPresent()) {
            UnionUser unionUser = optionalUnionUser.get();
            unionUser.setOrganizationId(null);
            unionUser.setOrganizationType(null);
            unionUser.setOrganizationName(null);
            unionUser.setOrganizationLicenseCode(null);

            return Optional.ofNullable(this.unionUserRepos.save(unionUser));
        }

        return Optional.empty();
    }

    @Override
    public UnionUser certifyUser(EndPointUser user, String residentId, String realName) {
        UnionUser unionUser = user.getUnionUser();

        if (unionUser == null) {
            unionUser = this.unionUserRepos.findByResidentId(residentId).orElse(new UnionUser());
        }

        if (realName != null)
            unionUser.setRealName(realName);
        if (residentId != null)
            unionUser.setResidentId(residentId);

        return this.unionUserRepos.save(unionUser);
    }
}
